package com.jshoperxms.controller.utils.sms;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;

/**
 * @author WCS.Wang
 * @version V1.0
 * @Package com.jshoperxms.controller.utils.sms
 * @Name miaokiss
 * @Description: 短信工具类
 * @date 2017/4/17
 */
public class SmsUtils {

    private static Logger logger = LoggerFactory.getLogger(SmsUtils.class);

    public static String ACCOUNT = "AB00167";

    public static String PASSWORD = "AB0016744";

    public static String URL = "http://dx.ipyy.net/sms.aspx?action=send&account=ACCOUNT&password=PASSWORD&mobile=MOBILE&content=CONTENT";

    // 发送短信
    public static void SendMessage(String mobile, String validate, String template) {
        // 使用StringBuffer的append获得xml形式的字符串
        StringBuffer sub = new StringBuffer();
        BufferedReader br = null;
        URL url = null;
        HttpURLConnection con;
        String line;
        Document document = null;
        Element rootElt = null;
        try {
            // 设置发送内容的编码方式
            String content = template.replace("@", validate);
            String send_content = URLEncoder.encode(content.replaceAll("<br/>", " "), "UTF-8");// 发送内容
            String requestUrl = SmsUtils.URL.replace("ACCOUNT", SmsUtils.ACCOUNT).replace("PASSWORD", SmsUtils.PASSWORD).replace("MOBILE", mobile).replace("CONTENT", send_content);
            System.out.println(requestUrl);
            url = new URL(requestUrl);
            con = (HttpURLConnection) url.openConnection();
            br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            while ((line = br.readLine()) != null) {
                sub.append(line + "");
            }
            document = DocumentHelper.parseText(sub.toString());
            rootElt = document.getRootElement();
            br.close();
            if (rootElt.elementText("returnstatus").trim().equals("Success")) {
                System.out.println("短信发送成功，状态码为：" + rootElt.elementText("returnstatus").trim());
            } else {
                System.out.println("短信发送错误，错误码为：" + rootElt.elementText("returnstatus").trim() + "，错误信息为：" + rootElt.elementText("message").trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("短信发送错误，错误码为：" + rootElt.elementText("returnstatus").trim() + "，错误信息为：" + rootElt.elementText("message").trim());
            logger.error("短信发送错误，错误码为：" + rootElt.elementText("returnstatus").trim() + "，错误信息为：" + rootElt.elementText("message").trim());
        } catch (DocumentException e) {
            e.printStackTrace();
            logger.error("解析Xml信息错误");
        } finally {
            logger.info("短信发送成功，状态码为：" + rootElt.elementText("returnstatus").trim());
        }
    }

    /**
     * 生成随机码
     *
     * @param num
     *            验证码位数
     * @return
     */
    public static String getPhoneValidate(int num) {
        StringBuffer buffer = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            buffer.append(random.nextInt(10));
        }
        return buffer.toString();
    }

}

